// pages/user/user.js
var WxParse = require('../../wxParse/wxParse.js');

var util = require('../../utils/util.js')

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    avatarDefault: '/images/avatar-default.png',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {




  },
  login: function (callback) {
    var that = this;
    app.login(function(e) {

      wx.getStorage({
        key:'userInfo',
        success:function(res){
          that.getUserinfo(res.data);
        }});

    });
  },

  getUserinfo: function (data) {
    var that = this;
    this.setData({
      userInfo: data,
      hasUserInfo: true,
    })

    WxParse.wxParse('article', 'html', data.nickname, that,5);

  },

  onOrderTap: function () {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/pages/orders/index',
      })
    } else {
      wx.showModal({
        title: '登录提示',
        content: '登录后才能查看哦！',
        showCancel: false
      })
    }
  },

  onCouponTap: function () {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/pages/coupons/index',
      })
    } else {
      wx.showModal({
        title: '登录提示',
        content: '登录后才能查看哦！',
        showCancel: false
      })
    }
  },

  onAddrTap: function () {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/pages/address/index',
      })
    } else {
      wx.showModal({
        title: '登录提示',
        content: '登录后才能查看哦！',
        showCancel: false
      })
    }
  },

  onAboutTap: function () {
    wx.showModal({
      title: '关于我们',
      content: '腾坤酒业商城V1.0！',
      showCancel: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // app.relateUser()
    var that = this;

    wx.getStorage({
      key:'userInfo',
      success:function(res){
        var userInfoStorage = res.data;
        if (!util.isNUll(userInfoStorage)&&!util.isNUll(userInfoStorage.userid)){
          app.globalData.userInfo = userInfoStorage;
          that.setData({
            userInfo: userInfoStorage,
            hasUserInfo: true,
          })
          WxParse.wxParse('article', 'html', userInfoStorage.nickname, that,5);

        } else {
          that.login();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '腾坤酒业',
      path: '/pages/home/index'
    }
  }
})